package com.pig4cloud.pig.module.service.wmj.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.CustomException;
import com.pig4cloud.pig.module.api.entity.excel.WmjIdCardExcel;
import com.pig4cloud.pig.module.api.entity.wmj.WmjDoor;
import com.pig4cloud.pig.module.api.entity.wmj.WmjIdCard;
import com.pig4cloud.pig.module.mapper.wmj.WmjDoorMapper;
import com.pig4cloud.pig.module.mapper.wmj.WmjIdCardMapper;
import com.pig4cloud.pig.module.service.wmj.IWmjIdCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微门禁 门禁卡Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-08
 */
@Service
public class WmjIdCardServiceImpl extends ServiceImpl<WmjIdCardMapper, WmjIdCard> implements IWmjIdCardService {

    @Resource
    private WmjDoorMapper wmjDoorMapper;
    @Resource
    private WmjIdCardMapper wmjIdCardMapper;
    @Override
    @Transactional
    public String importIdCardData(List<WmjIdCardExcel> templateList, Integer doorId, Boolean isUpdateSupport, String operName) {
        if (Validator.isNull(templateList) || templateList.size() == 0) {
            throw new CustomException("导入模板数据不能为空！");
        }
            WmjDoor door=wmjDoorMapper.selectById(doorId);
        if(door == null){
            throw new CustomException("所属门禁不存在！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WmjIdCardExcel template:templateList){
//            if (ObjectUtil.isNotEmpty(template.getIdCarNo()) || ObjectUtil.isNotEmpty(template.getPhoneNum())){
               if (template.getIdCardNo()!="" || template.getPhoneNum()!=""){
                    try {
                        if(template.getIdCardNo()!=null){
                            QueryWrapper<WmjIdCard> qw=new QueryWrapper<>();
                            if (template.getIdCardNo()!=null){
                                qw.eq("id_card_no",template.getIdCardNo());
                            }
                            if (template.getPhoneNum()!=null){
                                qw.eq("phone_num",template.getPhoneNum());
                            }
                            WmjIdCard IdCard = wmjIdCardMapper.selectOne(qw);
                            if (IdCard==null){
                                IdCard=new WmjIdCard();
                                IdCard.setState(1);
                                if (ObjectUtil.isNotEmpty(template.getIdCardNo()) || template.getIdCardNo()!=""){
                                    IdCard.setIdCardNo(template.getIdCardNo());
                                }
                                if (ObjectUtil.isNotEmpty(template.getPhoneNum()) || template.getPhoneNum()!=""){
                                    IdCard.setPhoneNum(template.getPhoneNum());
                                }
                                IdCard.setDoorId(doorId);
                                IdCard.setEstateId(door.getEstateId());
                                IdCard.setPropertyId(door.getPropertyId());
                                IdCard.setUserId(0);
                            }
                            wmjIdCardMapper.insert(IdCard);
                            successNum++;
                        }
                    }catch (Exception e){
                        String msg = "<br/>" + failureNum + "、卡号 " + template.getIdCardNo() + " 导入失败：";
                        failureMsg.append(msg + e.getMessage());
                        log.error(msg, e);
                    }
//                }
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
